package com.uniview.service.listener;

import com.uniview.eumn.CommonEnum;
import com.uniview.exception.CommonException;
import com.uniview.orm.daoX.ApiAuthorityMapper;
import com.uniview.orm.model.ApiAuthority;
import com.uniview.orm.model.ApiAuthorityExample;
import com.uniview.utils.TransSeqGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Author lux
 * @date: 2019/12/28
 * @desc:
 */
@Service
public class ScanService {
    @Autowired
    ApiAuthorityMapper apiAuthorityMapper;
    /**
     * @desc 保存auth到数据库
     * @param authMap
     * @param apiAuthority
     */
    public void saveAuth(ApiAuthority apiAuthority,Map<String,Object> authMap){
        apiAuthority.setApiMethod((String) authMap.get("methodName"));
        apiAuthority.setApiModule((String) authMap.get("serviceId"));
        apiAuthority.setApiName((String) authMap.get("apiName"));
        apiAuthority.setApiUri((String) authMap.get("md5"));
        apiAuthority.setIsOpen("0");//0：对外 1：不对外，默认不对外
        apiAuthority.setRequestType((String) authMap.get("contentType"));
        if(null==apiAuthority.getApiId()){
            //新增权限信息
            apiAuthority.setApiId(TransSeqGenerator.getId());
            apiAuthorityMapper.insertSelective(apiAuthority);
        }else{
            //更新权限信息
            ApiAuthorityExample apiAuthorityExample = new ApiAuthorityExample();
            apiAuthorityExample.createCriteria()
                    .andApiModuleEqualTo(authMap.get("serviceId").toString())
                    .andApiUriEqualTo(authMap.get("md5").toString())
                    .andApiMethodEqualTo(authMap.get("methodName").toString())
                    .andRequestTypeEqualTo(authMap.get("contentType").toString());
            apiAuthorityMapper.updateByExampleSelective(apiAuthority,apiAuthorityExample);
        }
    }

    /**
     * @desc 查询数据库的auth信息
     * @param authMap
     * @return apiAuthority
     */
    public ApiAuthority queryAuth(Map<String,Object> authMap){
        ApiAuthorityExample apiAuthorityExample = new ApiAuthorityExample();
        apiAuthorityExample.createCriteria()
                .andApiModuleEqualTo(authMap.get("serviceId").toString())
                .andApiUriEqualTo(authMap.get("md5").toString())
                .andApiMethodEqualTo(authMap.get("methodName").toString())
                .andRequestTypeEqualTo(authMap.get("contentType").toString());
        List<ApiAuthority> apiAuthorities =  apiAuthorityMapper.selectByExample(apiAuthorityExample);
        if(apiAuthorities.size()>1){
            //权限录入错误
            throw new CommonException(CommonEnum.FAIL_CODE, CommonEnum.FAIL_MSG);
        }else if(apiAuthorities.size()==0){
            return new ApiAuthority();
        }else {
            ApiAuthority apiAuthority = apiAuthorities.get(0);
            return apiAuthority;
        }
    }

}
